package org.xqp.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xqp.basic.util.PageList;
import org.xqp.basic.util.JsonResult;
import org.xqp.org.domain.Department;
import org.xqp.org.query.DepartmentQuery;
import org.xqp.org.service.IDepartmentService;

import java.util.List;

@RestController // @RestController这个标签的作用等于   @controller  和    @ResponseBody
@RequestMapping("/department")  // 请求映射路径
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    //交给spring管理以后就可以自动注入
    @Autowired
    private IDepartmentService departmentService;

    /**
     *
     * @param id
     * @return
     *  根据id查询数据库中的Department数据
     */
    @GetMapping("{id}") // @GetMapping("{id}"截取到get请求url地址中的id
    @ApiOperation(value = "根据id查询部门数据",notes = "传入一个id")
    public Department queryDepById(@PathVariable("id") Long id){  // @PathVariable("id")作用将@GetMapping("{id}")截取到的id传入参数id中
        return departmentService.findById(id);
    }

    /**
     * 查询所有的Department数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询部门全部数据",notes = "不需要参数")
    public List<Department> queryDepAll(){
        return departmentService.findAll();
    }

    /**
     * 查询的分页数据传给页面展示
     *          当前页 currentPage
     *          每页数据条数pageSize
     * @RequestBody作用将前端传来的json格式的数据转为自己定义好的javabean对象中
     */
    @PostMapping // 前端通过post方法传过来的数据在请求体中
    @ApiOperation(value = "根据前端传过来的当前页，每页数据条数，进行分页查询",notes = "传入DepartmentQuery对象")
    public PageList<Department> qureyPage(@RequestBody DepartmentQuery query){  // 将前端传过来json格式数据currentPage和pageSize封装到query对象中
        return departmentService.queryList(query);
    }

    /**
     * 根据id删除对象
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除",notes = "传入一个id")
    public JsonResult deleteDepById(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "传入装前台传过来的id数组")
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和更新方法
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public JsonResult addAndUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){ // 判断有没有id
                departmentService.insert(department); // 没有id就走添加方法insert
            }else {
                departmentService.update(department); // 有id就走更新方法update
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    @GetMapping("/tree")
    @ApiOperation(value = "查询部门等级",notes = "不需要传入参数")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
